<?php
defined('_O2PHP_EXEC') or die();
include_once (EMVC_PATH_LIBRARIES .'core/utils/common.php');
load('core.abstract.object');
load('core.loader');
class ESite extends EObject {
	/**
	 * MVC对应的模板、缓存、编译文件路径(数组方式存放)
	 *
	 * @var array
	 */
	var $files;
	/**
	 * 模块参数
	 *
	 * @var array
	 */
	var $auth_args=array();
	/**
	 * 构造函数
	 * 
	 */
	function __construct() {
		parent :: __construct();
		if(getCfg('session_domain','0') == '0'){
			session_set_cookie_params(0,'/',substr($_SERVER['HTTP_HOST'],strpos($_SERVER['HTTP_HOST'],'.')));
		}else{
			session_set_cookie_params(0,'/');
		}
		# 启用session
		$sess = Session::singleton();
		 
//		session_start();
//		$_SESSION['o2php']['pre_time'] = isset($_SESSION['o2php']['time'])?$_SESSION['o2php']['time']:time();
//		$_SESSION['o2php']['time'] = time();
		# 解析页面请求
		$this->parseRequest();
		# 设置时区
		ini_set('date.timezone', getCfg('timezone', 'Asia/Shanghai'));
		header('Content-type: text/html; charset="utf-8"');
		
		# 开户gzip
		$this->gzip();
	}
	function &singleton(){
		static $instance;
		if(!isset($instance)){
			$instance=new ESite();
		}
		return $instance;
	}
//	function rewrite_url($rule="default"){
//		if(isset($_GET['emvcurl'])){
//			switch($rule){
//				case "default":
//					$url = explode('/',$_GET['emvcurl']);
//					$m = (!$url[count($url)-2])?(getCfg('defaultModule')):($url[count($url)-2]);
//					$other = $url[count($url)-1];
//					$other = explode('.',$other);
//					$reqtype = (!$other[1])?(getCfg('defaultReqType','html')):($other[1]);
//					$args = array();
//					if(!$other[0]){
//						$a = getCfg('defaultAction');
//					}else{
//						$tmp =  explode('-',$other[0]);
//						if(count($tmp) == 1){
//							$a = $tmp[0];
//						}else{
//							$a = $tmp[0];
//							unset($tmp[0]);
//							$i = 0;
//							$args['emvc_arg'] = $tmp;
//						}
//					}
//					unset($_GET['emvcurl']);
//					$_GET['m'] = $m;
//					$_GET['a'] = $a;
//					$_GET['reqtype'] = $reqtype;
//					$_GET = array_merge($_GET,$args);
//				  //debug($_GET);
//					break;
//				//....	
//			}
//		}
//	}
	function parseRequest(){
//		# url rewrite过程
//		if(getCfg('rewrite_engine',false)){
//			$this->rewrite_url(getCfg('rewrite_rule','default'));
//		}
		$get = $_GET;
		ksort($get);
		$_GET[getCfg('ctl_name','m')] = !empty ($_GET[getCfg('ctl_name','m')]) ? strtolower($_GET[getCfg('ctl_name','m')]) : getCfg('defaultModule');
		$_GET[getCfg('act_name','a')] = !empty ($_GET[getCfg('act_name','a')]) ? strtolower($_GET[getCfg('act_name','a')]) : getCfg('defaultAction');
		$_GET['reqtype'] = !empty ($_REQUEST['reqtype']) ? strtolower($_REQUEST['reqtype']) : getCfg('defaultReqType');
		unset ($get[getCfg('ctl_name','m')], $get[getCfg('act_name','a')], $get['reqtype']);
		$_GET['eargs'] = $_REQUEST['eargs'] = $get;
		//$_REQUEST['uri'] = $_GET['uri'] = sefUri(strtolower($_GET['m']), strtolower($_GET['a']), $get, '-');
		//$_REQUEST['url'] = $_GET['url'] = url($_GET['m'], $_GET['a'], $get);
	}
	/**
	 * 执行模块
	 */
	function execute($module, $action, $args, $reqtype){
		$this->files = $this->filesHelper($module, $action, $args, $reqtype);
		$options = array(
			'files' => $this->files,
			'module' => $module,
			'action' => $action,
			'eargs' => $args,
			'reqtype' => $reqtype
		);
		if (file_exists($this->files['components'])) {
			include_once ($this->files['components']);
			$module_class = $module.'_controller';
			if (class_exists($module_class)) {
				$instance = & new $module_class ();
				 //debug($instance);
				//themeUrl 有问题
				$instance->setFrom($options);	
				if( !method_exists($instance,$action) ){
					$this->speedShow($options);
					exit();
				}
				$instance-> $action ();
				# 输出模板
				if ($instance->edisplay) {
					$instance->edisplay();
				}			
			} else {
				msgBox('出错:没有找到 ' . $module . ' 类.', '(-1)');
			}
		} else {
			// 没有发现control文件，现在进行直接模板输出
			$this->speedShow($options);
		}
		exit();
	}
	# 没有模块或没有模块方法，直接输出页面
	function speedShow($options){
		$instance = Controller::singleton();
		$instance->setFrom($options);	
		$instance->edisplay();
	}
	function filesHelper($module, $action, $args, $reqtype = 'html') {
		return $files = array (
		'components' => EMVC_PATH_APP.'controllers'.DS.$module.'_controller.php',
		'tpl' => EMVC_PATH_APP.getCfg('themes_dir','themes').DS.$module.DS.$action.'.php',
		'tpl_c' => EMVC_PATH_APP.getCfg('cache_data_dir','data').DS.$module.DS.$action.'.php', 
		'cache' => EMVC_PATH_APP.getCfg('cache_data_dir','data').DS.getCfg('cache_dir','cache').DS.$reqtype.DS.$module.DS.$action .'.php');
	}
	function gzip() {
		ini_set('zlib.output_compression', 1);
		ini_set('zlib.output_compression_level', 2);
	}
}
?>